package com.bhs.sansong.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.ScreenAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.AddressList;
import com.bhs.sansong.bean.ScreenEvent;
import com.bhs.sansong.bean.TabBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.DateUtils;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.view.AutoLineFeedLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.loper7.date_time_picker.DateTimeConfig;
import com.loper7.date_time_picker.dialog.CardDatePickerDialog;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class ScreenDialog extends Dialog implements View.OnClickListener, NetCallBack {
    private Context context;
    private BaseActivity baseActivity;
    private LinearLayout mLlScreen;
    private FrameLayout mFlClose;
    private LinearLayout mLlTime;
    private TextView mTvStartTime;
    private TextView mTvEndTime;
    private RecyclerView mRvStatus;
    private ScreenAdapter mStatusAdapter;
    private List<TabBean> statusList = new ArrayList<>();
    private RecyclerView mRvGoods;
    private ScreenAdapter mGoodsAdapter;
    private List<TabBean> goodsList = new ArrayList<>();
    private TextView mTvReset;
    private TextView mTvConfirm;

    public ScreenDialog(@NonNull BaseActivity context) {
        super(context, R.style.MyBottomDialogStyle);
        baseActivity = context;
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_screen);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();

        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
    }

    private void initView() {
        mLlScreen = findViewById(R.id.ll_screen);
        mFlClose = findViewById(R.id.fl_close);
        mLlTime = findViewById(R.id.ll_time);
        mTvStartTime = findViewById(R.id.tv_start_time);
        mTvEndTime = findViewById(R.id.tv_end_time);
        mRvStatus = findViewById(R.id.rv_status);
        mRvGoods = findViewById(R.id.rv_goods);
        mTvReset = findViewById(R.id.tv_reset);
        mTvConfirm = findViewById(R.id.tv_confirm);
        mTvReset.setOnClickListener(this);
        mTvConfirm.setOnClickListener(this);
        mFlClose.setOnClickListener(this);
        mTvStartTime.setOnClickListener(this);
        mTvEndTime.setOnClickListener(this);
        statusList.add(new TabBean("待付款", "0"));
        statusList.add(new TabBean("待接单", "3"));
        statusList.add(new TabBean("待揽收", "5"));
        statusList.add(new TabBean("待发车", "7"));
        statusList.add(new TabBean("运输中", "9"));
        statusList.add(new TabBean("待配送", "11"));
        statusList.add(new TabBean("待回单", "101"));
        statusList.add(new TabBean("已完成", "15,20"));
        statusList.add(new TabBean("已拒绝", "80"));
        statusList.add(new TabBean("已超时", "90"));
        statusList.add(new TabBean("已取消", "99"));
        statusList.add(new TabBean("全部", ""));
        baseActivity.networkRequest(baseActivity.netApi.get_order_common_data(), this, 9527);
        mStatusAdapter = new ScreenAdapter(statusList);
        mRvStatus.setAdapter(mStatusAdapter);
        mRvStatus.setLayoutManager(new AutoLineFeedLayoutManager());

        mStatusAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (TabBean tabBean : statusList) {
                    tabBean.setChecked(false);
                }
                statusList.get(position).setChecked(true);
                mStatusAdapter.notifyDataSetChanged();
            }
        });
    }

    private ScreenListener screenListener;

    public void setMyClickListener(@Nullable ScreenListener l) {
        this.screenListener = l;
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        AddressList bean = new Gson().fromJson(json, AddressList.class);
        if (bean.isStatus()) {
            for (String s : bean.getData().getGoods_type()) {
                goodsList.add(new TabBean(s, ""));
            }
            mGoodsAdapter = new ScreenAdapter(goodsList);
            mRvGoods.setAdapter(mGoodsAdapter);
            mRvGoods.setLayoutManager(new AutoLineFeedLayoutManager());
            mGoodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @SuppressLint("NotifyDataSetChanged")
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    for (TabBean tabBean : goodsList) {
                        tabBean.setChecked(false);
                    }
                    goodsList.get(position).setChecked(true);
                    mGoodsAdapter.notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
    private int timeType;
    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_start_time) {
            DatePickerDialogShow(1);
        } else if (view.getId() == R.id.tv_end_time) {
            DatePickerDialogShow(2);
        } else if (view.getId() == R.id.tv_reset) {
            mTvStartTime.setText("");
            mTvEndTime.setText("");
            for (TabBean datum : goodsList) {
                datum.setChecked(false);
            }
            for (TabBean tabBean : statusList) {
                tabBean.setChecked(false);
            }
            mStatusAdapter.notifyDataSetChanged();
            mGoodsAdapter.notifyDataSetChanged();
        } else if (view.getId() == R.id.tv_confirm) {
            dismiss();
            confirm();
        } else if (view.getId() == R.id.fl_close) {
            dismiss();
        }
    }

    private void confirm() {

        String goods = "";
        String status = "";
        for (TabBean datum : goodsList) {
            if (datum.isChecked()) {
                goods = datum.getTypeName();
            }
        }
        for (TabBean tabBean : statusList) {
            if (tabBean.isChecked()) {
                status = tabBean.getType();
            }
        }

        screenListener.onClick(new ScreenEvent(mTvStartTime.getText().toString(), mTvEndTime.getText().toString(), goods, status));
    }

    private void DatePickerDialogShow(int type) {
        String title;
        if (timeType == 1) {
            title = "设置开始时间";
        } else {
            title = "设置结束时间";
        }
        new CardDatePickerDialog.Builder(context)
                .setTitle(title)
                .showBackNow(false)
                .setDisplayType(DateTimeConfig.YEAR, DateTimeConfig.MONTH, DateTimeConfig.DAY)
                .setThemeColor(Color.parseColor("#485EF4"))
                .setOnChoose("确定", aLong -> {
                    if (timeType == 1) {
                        mTvStartTime.setText(DateUtils.getYearMonthDay(aLong));
                    } else {
                        mTvEndTime.setText(DateUtils.getYearMonthDay(aLong));
                    }
                    return null;
                }).build().show();
        timeType = type;
    }

}
